package com.evan.review.launchmode;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * 栈顶复用模式(singleTop)的Activity演示
 * 
 * 特点：
 * 1. 如果要启动的Activity已经位于栈顶，则不会创建新实例，而是复用栈顶实例，并调用onNewIntent()方法
 * 2. 如果要启动的Activity不在栈顶，则会创建新的实例
 */
public class SingleTopActivity extends AppCompatActivity {
    
    private static final String TAG = "SingleTopActivity";
    private TextView tvInfo;
    private int instanceCount = 0;
    private static int totalInstance = 0;
    private int onNewIntentCount = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singletop);
        
        // 记录实例数量
        instanceCount = ++totalInstance;
        
        initViews();
        updateInfo();
        
        Log.d(TAG, "onCreate: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
    
    private void initViews() {
        tvInfo = findViewById(R.id.tv_info);
        
        Button btnCreateSelf = findViewById(R.id.btn_create_self);
        btnCreateSelf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 启动自身，测试栈顶复用
                Intent intent = new Intent(SingleTopActivity.this, SingleTopActivity.class);
                startActivity(intent);
            }
        });
        
        Button btnCreateStandard = findViewById(R.id.btn_create_standard);
        btnCreateStandard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 先启动一个标准Activity，然后再回来看是否会创建新实例
                Intent intent = new Intent(SingleTopActivity.this, StandardActivity.class);
                startActivity(intent);
            }
        });
        
        Button btnCreateStandardThenSelf = findViewById(R.id.btn_create_standard_then_self);
        btnCreateStandardThenSelf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 先启动Standard，再从Standard启动自己
                Intent intent = new Intent(SingleTopActivity.this, StandardActivity.class);
                intent.putExtra("shouldLaunchSingleTop", true);
                startActivity(intent);
            }
        });
        
        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    
    private void updateInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("【栈顶复用模式演示】\n\n");
        sb.append("当前Activity: ").append(this.getClass().getSimpleName()).append("\n");
        sb.append("实例Hash: ").append(this.hashCode()).append("\n");
        sb.append("实例编号: ").append(instanceCount).append("/").append(totalInstance).append("\n");
        sb.append("onNewIntent调用次数: ").append(onNewIntentCount).append("\n");
        sb.append("任务栈ID: ").append(getTaskId()).append("\n\n");
        
        sb.append("特点说明:\n");
        sb.append("1. 栈顶复用模式下，如果该Activity已在栈顶，则复用该实例并调用onNewIntent()\n");
        sb.append("2. 如果该Activity不在栈顶，则会创建新的实例\n");
        sb.append("3. 点击\"启动自身\"按钮，因为当前Activity在栈顶，不会创建新实例，但会调用onNewIntent()\n");
        sb.append("4. 点击\"启动Standard\"后，再通过Standard的\"返回\"按钮回到本Activity，不会创建新实例\n");
        sb.append("5. 点击\"启动Standard并返回自身\"，因为不在栈顶，会创建新的实例\n\n");
        
        sb.append("日志查看提示:\n");
        sb.append("可在Logcat中过滤\"SingleTopActivity\"查看详细信息");
        
        tvInfo.setText(sb.toString());
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        onNewIntentCount++;
        Log.d(TAG, "onNewIntent: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
        updateInfo();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
} 